<?php

namespace App\Services\UpdateUserInfo;

use App\Models\User;
use Illuminate\Http\Request;
use Zhuzhichao\BankCardInfo\BankCard as BankCardInterface;
use Illuminate\Support\Facades\Redis;
use App\Services\UserInviteInfo\MakeUserAvatar;


class UpdateUserInfo
{
	protected Request $request;

	public function __construct(Request $request)
	{
		$this->request = $request;
	}

	protected function updateData()
	{
        $request = $this->request;

        $bankCard = str_replace(" ","",$request->bank_card);
        if($bankCard){

            $result = file_get_contents("https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=".$bankCard."&cardBinCheck=true");
            $result = json_decode($result);

            $bankList = BankCardInterface::getBankList();

            if($result->validated == false){
                fail('银行卡号错误！');

            }
            if(!isset($bankList[$result->bank])){

                fail('该银行暂不支持，请绑定其他银行卡');
            }
            
            $card_info = BankCardInterface::info($bankCard);
            if($card_info['validated'] == false) fail('银行卡号错误或不支持该银行');
          
        }
        /**
         * @var User $user
         */
        $user = $request->user();
       

        if(request('avatar')){
            $avatar = app(MakeUserAvatar::class, [
                        'name' => $user->name,
                        'id' => $user->id,
                        'avatar'=>$request->avatar,
                    ])->make();
        }else{
            $avatar = $user->avatar;

        }

        $data = [
            'name' => $request->name ?? $user->name,
            'avatar' => $avatar,
            'signature' => $request->signature ?? $user->signature,
            'gender' => $request->gender ?? $user->gender,
            'birthday' => $request->birthday ?? $user->birthday,
            'alipay_user_no' => $request->alipay_user_no ?? $user->alipay_user_no,
            'is_locked' => $request->is_locked ?? $user->is_locked,
            'wx_image' => $request->wx_image ?? $user->wx_image,
            'alipay_real_name' => $request->alipay_real_name ?? $user->alipay_real_name,
            'alipay_number' => $request->alipay_number ?? $user->alipay_number,
            'real_name' => $request->real_name ?? $user->real_name,
            'contact_phone' => request()->has('contact_phone') ? $request->contact_phone : $user->contact_phone,
            'is_show_support' =>$request->is_show_support ?? $user->is_show_support,
            'wechat_account' => request()->has('wechat_account') ? $request->wechat_account : $user->wechat_account,
            'qq_account' => $request->qq_account ?? $user->qq_account,
            'is_test' => $request->is_test ?? $user->is_test,
            'has_command' => $request->has_command ?? $user->has_command,
        ];

        
        if($bankCard){
            $data['cert_number'] = $bankCard;
            $data['bank_abbr'] = $card_info['bank'];
            $data['bank_name'] = $card_info['bankName'];
          
        }

        if ($inviteCode = $request->get('invite_code', null)) {
            $parent = User::query()->where("invite_code",$inviteCode)->first();
            $data['parent_id'] = $parent->id;
            $data['parent_name'] = $parent->name;




            //Redis::hMSet($user->infoCacheKey(), ['parent_name',$parent->name]);
           
        }

		$user->update($data);
        if($user->parent_id && !empty($request->get('invite_code'))){

             $user->addTeamRelationUser();
             user_upgrade($parent);
        }
	}

	public function update()
	{
		$this->updateData();
	}
}
